package cn.iocoder.yudao.module.kt.controller.admin.digitalimg;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.kt.controller.admin.digitalimg.vo.*;
import cn.iocoder.yudao.module.kt.dal.dataobject.digitalimg.DigitalImgDO;
import cn.iocoder.yudao.module.kt.service.digitalimg.DigitalImgService;

@Tag(name = "管理后台 - 数字人形象管理")
@RestController
@RequestMapping("/kt/digital-img")
@Validated
public class DigitalImgController {

    @Resource
    private DigitalImgService digitalImgService;

    @PostMapping("/create")
    @Operation(summary = "创建数字人形象管理")
    @PreAuthorize("@ss.hasPermission('kt:digital-img:create')")
    public CommonResult<Long> createDigitalImg(@Valid @RequestBody DigitalImgSaveReqVO createReqVO) {
        return success(digitalImgService.createDigitalImg(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数字人形象管理")
    @PreAuthorize("@ss.hasPermission('kt:digital-img:update')")
    public CommonResult<Boolean> updateDigitalImg(@Valid @RequestBody DigitalImgSaveReqVO updateReqVO) {
        digitalImgService.updateDigitalImg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数字人形象管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('kt:digital-img:delete')")
    public CommonResult<Boolean> deleteDigitalImg(@RequestParam("id") Long id) {
        digitalImgService.deleteDigitalImg(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数字人形象管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('kt:digital-img:query')")
    public CommonResult<DigitalImgRespVO> getDigitalImg(@RequestParam("id") Long id) {
        DigitalImgDO digitalImg = digitalImgService.getDigitalImg(id);
        return success(BeanUtils.toBean(digitalImg, DigitalImgRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数字人形象管理分页")
    @PreAuthorize("@ss.hasPermission('kt:digital-img:query')")
    public CommonResult<PageResult<DigitalImgRespVO>> getDigitalImgPage(@Valid DigitalImgPageReqVO pageReqVO) {
        PageResult<DigitalImgDO> pageResult = digitalImgService.getDigitalImgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DigitalImgRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数字人形象管理 Excel")
    @PreAuthorize("@ss.hasPermission('kt:digital-img:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDigitalImgExcel(@Valid DigitalImgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DigitalImgDO> list = digitalImgService.getDigitalImgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数字人形象管理.xls", "数据", DigitalImgRespVO.class,
                        BeanUtils.toBean(list, DigitalImgRespVO.class));
    }

}